package com.nq.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.nq.common.interceptor.ApiAdminAuthorityInterceptor;
import com.nq.common.interceptor.ApiAgentAuthorityInterceptor;
import com.nq.common.interceptor.ApiUserAuthorityInterceptor;
import com.nq.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {


    @Value("${spring.jackson.date-format}")
    private String dateFormat;


    @Value("${spring.jackson.time-zone}")
    private String timeZone;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins("*")
//                .allowedMethods("*")
//                .allowCredentials(true)  // 允許帶cookie訪問
//                .allowedHeaders("*")
//                .maxAge(3600);
//                ;
//
//    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/order_voucher/**").addResourceLocations("file:/order_voucher/");
    }

    //註冊攔截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //SpringMVC下，攔截器的註冊需要排除對靜態資源的攔截(*.css,*.js)
        //SpringBoot已經做好了靜態資源的映射，因此我們無需任何操作
        registry.addInterceptor(new ApiAgentAuthorityInterceptor()).addPathPatterns("/agentNew/**")
                .excludePathPatterns("/index.html", "/", "/user/login")
        ;
        registry.addInterceptor(new ApiUserAuthorityInterceptor(redisTemplate)).addPathPatterns("/user/**")
                .excludePathPatterns("/index.html", "/", "/user/login")
        ;
        registry.addInterceptor(new ApiAdminAuthorityInterceptor()).addPathPatterns("/admin/**")
                .excludePathPatterns("/index.html", "/", "/user/login")
        ;

    }

    /**
     * 全局设置日期格式化
     * @return
     */
    @Bean
    public ObjectMapper jacksonObjectMapperCustomization() {
        System.out.println("==============================");
        SimpleDateFormat format = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
                .timeZone(timeZone)
                .dateFormat(format);
        return builder.build();
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.removeIf(c -> c instanceof MappingJackson2HttpMessageConverter);
        converters.add(new MappingJackson2HttpMessageConverter(jacksonObjectMapperCustomization()));
    }
}
